<template>
    <view class="globe_back">
        <view class="box">
            <view class="address flex flex_wrap justify_content_between">
                <view style="width: 95%" @click="address" v-if="!order_info.address">
                    <view class="addTitle">收货地址</view>
                    <view class="addText">请选择或添加您的收货地址</view>
                </view>
				<view style="width: 95%" @click="address" v-else>
				    <view ><span class="addTitle">{{order_info.address.real_name}}</span><span>{{order_info.address.phone}}</span></view>
				    <view class="addText">{{order_info.address.province}}{{order_info.address.city}}{{order_info.address.district}}{{order_info.address.detail}}</view>
				</view>
                <view style="width: 5%">
                    <image class="icon_r" src="https://jiuyuan.smomotbo.com/static/imgs/query-info/next-icon.png" mode="widthFix"></image>
                </view>
            </view>
            <view class="list" v-if="order_info.order">	
                <view class="flex" v-for="(item,index) in order_info.order[0].specifications" :key="item.id">
                    <view>
                        <image class="ProductMap" :src="item.product_image" mode="widthFix"></image>
                    </view>
                    <view class="rightBox">
                        <view class="title">{{ item.product_name }}</view>
                        <view class="option">规格：{{ item.guige }}</view>
                        <view class="flex justify_content_between">
                            <view class="money">￥{{ item.money }}</view>
                            <u-number-box :asyncChange="true" v-model="item.num" :name="index" @change="valChange" buttonSize="26"></u-number-box>
                        </view>
                    </view>
                </view>
            </view>
            <!-- 留言 -->
            <view class="coupon">
                <view class="flex">
                    <text class="couT">留言</text>
                    <input class="line_F7FAFF coutext" type="text" placeholder="请填写您的留言" />
                </view>
              <!--  <view class="flex">
				  <view class="couT">优惠券</view>
				  <view class="flex line_F7FAFF justify_content_between coutext" @click="coupons()">
					<input type="text" class="cl_00091A" disabled v-model="Coupon" placeholder="请选择优惠券" />
					<image class="couponIcon" src="https://jiuyuan.smomotbo.com/static/imgs/query-info/next-icon.png" mode="widthFix"></image>
				  </view>
				</view> -->
            </view>
            <!-- 金额 -->
            <view class="detailedBox">
            <view class="flex justify_content_between detailed" v-for="item in order_info.product">
			  <view>{{item.name}}</view>
			  <view>￥{{item.money}}</view>
			</view>
			<view class="flex justify_content_between detailed" v-if="product_type==1">
				<view>邮费</view>
				<view>包邮</view>
			</view>
			<view class="flex justify_content_between detailed" v-if="product_type==3">
				<view>安装费</view>
				<view>{{order_info.config[0].value}}</view>
			</view>

			<view class="flex justify_content_between detailed" v-if="product_type==3">
				<view>配送费</view>
				<view>{{order_info.config[1].value}}</view>
			</view>
			<view class="flex justify_content_between detailed">
				<view>优惠</view>
				<view>￥0</view>
			</view>
			<view class="flex justify_content_between detailed">
				<view>合计</view>
				<view style="color: #ff6401">￥{{ order_info.money }}</view>
			</view>
            </view>
        </view>
        <!-- 底部 -->
        <view class="button flex justify_content_between">
            <view class="TotalPrice">
                <view class="f_24">
                    合计：
                    <text style="color: #ff6401; font-size: 32rpx; font-weight: bold">{{ order_info.money }}</text>
                </view>
                <view class="cl_999CA3 f_24 mt_16">
                    共{{ order_info.num }}件 优惠
                    <text class="cl_00091A">￥{{counpon_money}}</text>
                </view>
            </view>
            <view class="buy" @click="buy">立即付款</view>
        </view>
        <!-- 优惠券弹窗 -->
        <u-popup :show="couponShow" :round="10" mode="bottom" @close="close" @open="open">
            <view>
                <u-radio-group v-model="radiovalue1" placement="column" @change="groupChange" iconPlacement="right">
                    <u-radio
                        :customStyle="{
                            margin: '8px 16px',
                            background: '#FFEFE5',
                            padding: '16px'
                        }"
                        v-for="(item, index) in coupon"
                        :key="index"
                        :label="item.name"
                        :name="item.name"
                        @change="radioChange"
                    >
                        <view class="flex couponBox">
                            <view class="discount">
                                <view style="font-size: 52rpx">{{ item.title }}</view>
                                <view>{{ item.threshold }}</view>
                            </view>
                            <view class="title">
                                <view>{{ item.name }}</view>
                                <view class="time">有效期至{{ item.time }}</view>
                            </view>
                        </view>
                    </u-radio>
                    <view class="couponBtn" @click="submit">确认</view>
                </u-radio-group>
            </view>
        </u-popup>
    </view>
</template>

<script>
import { getOrderInfo, memberInfo, transfers } from '@/api';
export default {
    data() {
        return {
            order_id: 0,
            order_info: { order_id: 0 },
            value: 1,
            couponShow: false,
            Coupon: '',
			counpon_money:0,
			product_type:0,
            // 优惠券
            coupon: [
                {
                    name: '新人优惠券',
                    time: '2023-12-15',
                    title: '7折',
                    threshold: '无使用门槛',
                    disabled: false
                },
                {
                    name: '会员券',
                    time: '2023-12-25',
                    title: '8折',
                    threshold: '无使用门槛',
                    disabled: false
                }
            ]
        };
    },
    onLoad(args) {
        console.debug('args', args);
        if (args.order_id) {
            this.order_id = args.order_id;
            this.order_info.order_id = args.order_id;
			this.product_type = args.product_type;
            this.handleGetOrderInfo();
        } else {
            this.$u.route({ type: 'back' });
        }
    },
    onShow() {

		this.order_info.address=this.$.get_data("current_address")
		// console.log(this.$.get_data("current_address"))
    },
    methods: {
        handleGetOrderInfo() {
			
			this.$.ajax(1,"POST","index/pay_order_index",{
				order_id:this.order_id
			},(res)=>{
			  if(res.code==200){
					this.order_info=res.data;
					if(this.product_type==3){
						this.order_info.money=Number(this.order_info.money)+Number(this.order_info.config[0].value)+Number(this.order_info.config[1].value)
						this.order_info.money=Number(this.order_info.money)-Number(this.counpon_money)
					}
					if(this.product_type==1){
						this.order_info.money=Number(this.order_info.money)-Number(this.counpon_money)
					}
			  }else{
			     this.$.ti_shi(res.msg);
			  }
			})
            // getOrderInfo(this.order_id).then(({ data }) => {
            //     this.order_info = data;
            // });
        },
		valChange(e){
			console.log(e)
			// var that = this
			this.order_info.order[0].specifications[e.name].num=e.value;
			
			// this.order_info.money=Number(this.order_info.order[0].specifications[e.name].money)*Number(this.order_info.order[0].specifications[e.name].num)
			let moneys=0;
			this.order_info.order[0].specifications.forEach(function (items, indexs) {
				moneys=moneys+(Number(items.money)*Number(items.num));
				
			});
			this.order_info.money=moneys
		},
        coupons() {
			if(this.order_info.usercoupon.join(',')){
				this.couponShow = true
			}else{
				this.$.ti_shi('暂无优惠券')
			}
			
            // transfers({ id: this.order_id, address_meta: this.$current_address }).then((res) => {
            //     console.log('transfers res: ', res);
            //     uni.$lz.route({ url: '/pages/index/success', type: 'redirect' });
            // });
        },
        submit() {
            this.couponShow = false;
        },
        groupChange(n) {
            console.log('groupChange', n);
            this.Coupon = n;
        },
        address() {
            uni.navigateTo({
                url: '/pages/me/Address?type=1'
            });
        },
		buy() {
			
			if(!this.order_info.address){
				this.$.ti_shi('请选择收货地址');
				return;
			}
		    this.$.ajax(1,"POST","index/pay_order",{
		    	order_id:this.order_id,
				address:this.order_info.address,
				money:this.order_info.money,
				counpon_money:this.counpon_money,
				product_type:this.product_type,
		    },(res)=>{
		      if(res.code==200){
				  var that =this
				   uni.requestPayment({
				   	provider: "wxpay",
				   	timeStamp: res.data.timeStamp,
				   	nonceStr: res.data.nonceStr,
				   	package: res.data.package,
				   	signType: res.data.signType,
				   	paySign: res.data.paySign,
				   	success: function (ress) {
				   		setTimeout(() => {
				   			uni.navigateBack({
				   				delta:1
				   			})
				   		}, 1000);
				   	},
				   	fail: function (err) {
				   		console.log(err)
				   		that.$.ti_shi(err)
				   					
				   	}
				   });
				  // this.$.ti_shi(res.msg);
				  // setTimeout(() => {
				  //     uni.switchTab({
				  //     	url:'/pages/home/me'
				  //     })
				  // }, 1000);
		    		
		      }else{
		         this.$.ti_shi(res.msg);
		      }
		    })
		},
		
    }
};
</script>

<style scoped lang="scss">
.box {
    padding: 32rpx;
    .addTitle {
        font-size: 40rpx;
        color: #00091a;
        font-weight: bold;
    }
    .addText {
        font-size: 28rpx;
        color: #999ca3;
        margin-top: 16rpx;
    }
    .icon_r {
        width: 32rpx;
        height: 32rpx;
        vertical-align: middle;
        margin-top: 36rpx;
    }
    .address {
        background: white;
        border-radius: 16rpx;
        padding: 24rpx;
    }
    .list,
    .coupon,
    .detailedBox {
        background: white;
        border-radius: 16rpx;
        padding: 24rpx;
        margin-top: 24rpx;
        .title {
            width: 100%;
            font-size: 32rpx;
            color: #00091a;
            font-weight: bold;
            display: block;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            margin-left: 16rpx;
        }
        .option {
            color: #999ca3;
            font-size: 24rpx;
            background: #f7faff;
            padding: 16rpx;
            border-radius: 16rpx;
            display: inline-block;
            margin-left: 16rpx;
            margin-top: 16rpx;
        }
        .money {
            color: #ff6401;
            font-size: 32rpx;
            margin-left: 16rpx;
            margin-top: 16rpx;
        }
        .rightBox {
            width: 70%;
        }
    }
    .ProductMap {
        width: 200rpx;
        height: 200rpx;
        border-radius: 16rpx;
    }
    .couT {
        width: 30%;
        margin: 16rpx 0;
        color: #333a47;
        font-size: 28rpx;
    }
    .coutext {
        width: 70%;
        padding: 16rpx 0;
        font-size: 28rpx;
        // color: #F1F1F3;
    }
    .couponIcon {
        width: 32rpx;
        height: 32rpx;
        vertical-align: middle;
    }
    .detailed {
        color: #333a47;
        font-size: 28rpx;
        margin-bottom: 32rpx;
    }
}
.button {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    padding: 16rpx 32rpx 50rpx;
    background: #ffffff;
    .buy {
        width: 176rpx;
        height: 76rpx;
        line-height: 76rpx;
        text-align: center;
        color: white;
        border-radius: 8rpx;
        background-image: linear-gradient(to right, #ff924d, #ff6401);
    }
}
.couponBox {
    .discount {
        color: #ff6401;
        font-size: 24rpx;
        text-align: center;
    }
    .title {
        color: #00091a;
        font-size: 32rpx;
        margin-left: 66rpx;
    }
    .time {
        color: #999ca3;
        font-size: 28rpx;
        display: block;
        margin-top: 16rpx;
    }
}
.couponBtn {
    margin: 24rpx auto;
    width: 686rpx;
    height: 100rpx;
    border-radius: 24rpx;
    background: #3377ff;
    text-align: center;
    line-height: 100rpx;
    color: white;
    font-size: 32rpx;
    font-weight: bold;
}
</style>
